<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/11/30
 * Time: 9:16
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Activitytimes extends Base
{
    /**
     * 拍卖活动场次
     * @return mixed|\think\response\Json
     */
    public function paiMaiIndex(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('activity_times')->where('at_type', 2)->count();// 获取总条数
            $lists = Db::name('activity_times')
                ->where('at_type', 2)
                ->field('at_id, at_start_time, at_end_time, at_num')
                ->page($Nowpage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('activitytimes/paimaiindex');
    }
    
    /**
     * 秒杀活动场次
     * @return mixed|\think\response\Json
     */
    public function miaoShaIndex(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('activity_times')->where('at_type', 3)->count();// 获取总条数
            $lists = Db::name('activity_times')
                ->where('at_type', 3)
                ->field('at_id, at_start_time, at_end_time, at_num')
                ->page($Nowpage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch('activitytimes/miaoshaindex');
    }
    
    /**
     * 编辑拍卖商品可参加活动商品数
     * @return mixed|\think\response\Json
     */
    public function pTimesEdit(){
        $at_id = input('at_id');
        if(Request::instance()->isPost()){
            $at_num = input('at_num');
            if($at_num == 0){
                return json(['code' => 100, 'msg' => '不可设置为0']);
            }
            $res = Db::table('up_activity_times')->where('at_id', $at_id)->update([
                'at_num' => $at_num,
                'at_updatetime' => date('Y-m-d H:i:s')
            ]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $activity = Db::table('up_activity_times')->where('at_id', $at_id)->field('at_num')->find();
        $this->assign([
            'at_id' => $at_id,
            'activity' => $activity
        ]);
        return $this->fetch('activitytimes/ptimesedit');
    }
    
    /**
     * 编辑秒杀商品可参加活动商品数
     * @return mixed|\think\response\Json
     */
    public function mTimesEdit(){
        $at_id = input('at_id');
        if(Request::instance()->isPost()){
            $at_num = input('at_num');
            if($at_num == 0){
                return json(['code' => 100, 'msg' => '不可设置为0']);
            }
            $res = Db::table('up_activity_times')->where('at_id', $at_id)->update([
                'at_num' => $at_num,
                'at_updatetime' => date('Y-m-d H:i:s')
            ]);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $activity = Db::table('up_activity_times')->where('at_id', $at_id)->field('at_num')->find();
        $this->assign([
            'at_id' => $at_id,
            'activity' => $activity
        ]);
        return $this->fetch('activitytimes/mtimesedit');
    }
}